$(function(){
	columnEdit.init();
});

var columnEdit=function(){
	var firstGrid,secondGrid;
	var storage=JSON.parse(localStorage.templateBuilder);
	var init=function(){
		
		// _loadData();
		if(storage.mode=='oneQuery'){
			//如果当前模式是单表查询的话，将选择列的内容变成两列，一列是搜索列，另一列是显示列
			$('.panel-body').find('.list-wrap:first').addClass('two-col')
				.find('.list-header').text('搜索列');
			oneQueryEvent();
		}else if(storage.mode=='oneEdit'){
			oneEditEvent();
		}
		_initGrid();
		//将上一步的路径更改为selectTable.html?mode=oneQuery
		$('#pre').attr('href',$('#pre').attr('href')+'?origin='+storage.origin);
	};
	var _loadData=function(){
		setTimeout(function(){
			$('.panel-body').find('.spinner').addClass('hide')
				.siblings().removeClass('hide');
		},2000);
	};
	//生成mmgrid对象
	var _initGrid=function(){
		var mode=storage.mode;
		var gridWidth=760;
		if(mode=='oneQuery'){
			gridWidth=gridWidth/2;
		}
		//测试数据
		var items=[
			{
				columnName:'EOOR_ID',
				columnComment:'角_色表主键',
				type:'INTEGER'
			},
			{
				columnName:'EOOR_REQUIRE_CONSIGNEE_TIME_TO',
				columnComment:'要求送达时间到',
				type:'DATE'
			},
			{
				columnName:'EOOR_CONSIGNEE_NAME',
				columnComment:'收货人名称',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_SHIPPER_DISTRICT_CODE',
				columnComment:'提货县区代码',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_CONSIGNEE_LOCATION_CODE',
				columnComment:'送达地点代码',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_PRIORITY_NAME',
				columnComment:'优先级名称',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_CUSTOMER_NAME',
				columnComment:'客户名称',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_CUSTOMER_NAME',
				columnComment:'客户名称',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_CUSTOMER_NAME',
				columnComment:'客户名称',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_CUSTOMER_NAME',
				columnComment:'客户名称',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_CUSTOMER_NAME',
				columnComment:'客户名称',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_CUSTOMER_NAME',
				columnComment:'客户名称',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_CUSTOMER_NAME',
				columnComment:'客户名称',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_CUSTOMER_NAME',
				columnComment:'客户名称',
				type:'VARCHAR2'
			},
			{
				columnName:'EOOR_DO_NO',
				columnComment:'提货单号',
				type:'VARCHAR2'
			}
		];
		//表头
		var cols=[
			{title:'列名',name:'columnName',width:100,sortable: true, type:'string', align:'left'},
			{title:'列注释',name:'columnComment',width:100,sortable: true, type:'string', align:'left'},
			{title:'类型',name:'type',width:100,sortable: true, type:'string', align:'left'}
		];
		firstGrid=$('#firstGrid').mmGrid({
			width:'auto',
			height: 430,
			cols: cols, 
            items: items,
            checkCol:true,
            fullWidthRows:true,
            nowrap : true,
            multiSelect:true
		});
		secondGrid=$('#secondGrid').mmGrid({
			width:'auto',
			height: 430,
			cols: cols, 
            items: items,
            checkCol:true,
            fullWidthRows:true,
            nowrap : true,
            multiSelect:true
		});

	};
	// storage.editCols=[];
	//‘单表编辑’模式下
	var oneEditEvent=function(){
		$('#next').on('mousedown',function(e){
			//刚进页面时，被选择的列置为空
			storage.editCols=[];
			//获取被checked的多选框
			var selectedRows=firstGrid.selectedRows();
			if(selectedRows.length>0){
				//初始化
				storage.editCols=[];
				//遍历选中的行，将列名加入storage.editCols
				for(var i in selectedRows){
					var row=selectedRows[i];
					var data={
						order:i,
						title:row.columnName,
						column:row.columnName
					};
					storage.editCols.push(data);
				}
				//将storage放入本地存储中
				localStorage.templateBuilder=JSON.stringify(storage);
			}else{
				alert('Not Checked!');
			}
		});
	};
	//‘单表查询’模式下
	var oneQueryEvent=function(){
		$('#next').on('mousedown',function(e){
			//清空数据
			storage.queryCols=[];
			storage.showCols=[];
			//获取被checked的多选框
			var queryCols=firstGrid.selectedRows();
			var showCols=secondGrid.selectedRows();
			if(queryCols.length>0&&showCols.length>0){
				for(var i in queryCols){
					var row=queryCols[i];
					storage.queryCols.push(row.columnName);
				}
				for(var i in showCols){
					var row=showCols[i];
					storage.showCols.push(row.columnName);
				}
				//将storage放入本地存储中
				localStorage.templateBuilder=JSON.stringify(storage);
			}else{
				alert('搜索列和显示列都必须至少选择一个字段');
			}
		});
	};
	return{
		init:init,
		oneEdit:oneEditEvent,
		oneQuery:oneQueryEvent
	}
}();